﻿var mapping={"A":65,"B":66,"C":67,"D":68,"E":69,"F":70,"G":71,"H":72,"I":73,"J":74,"K":75,"L":76,"M":77,"N":78,"O":79,"P":80,"Q":81,"R":82,"S":83,"T":84,"U":85,"V":86,"W":87,"X":88,"Y":89,"Z":90,
"0":48,"1":49,"2":50,"3":51,"4":52,"5":53,"6":54,"7":55,"8":56,"9":57,
"F1":112,"F2":113,"F3":114,"F4":115,"F5":116,"F6":117,"F7":118,"F8":119,"F9":120,"F10":121,"F11":122,"F12":123,"F13":124,"F14":125,"F15":126,
"Backspace":8,"Tab":9,"Clear":12,"Enter":13,"Shift":16,"Control":17,"Alt":18,"CapsLock":20,"Esc":27,"空格键":32,"PageUp":33,"PageDown":34,"End":35,"Home":36,
"左箭头":37,"向上箭头":38,"右箭头":39,"向下箭头":40,"Insert":45,"Delete":46,"Help":47,"+":61,"-":173};

	var keyClickStatus={"52":true};
  var selectCarrierChain=null; 
	
 var lastSelectCarrierChain=null; 
 
  var	onMouseDownX = 0;
	var	onMouseDownY = 0;
	
	var onMouseUpX =0;
	var	onMouseUpY =0;
	function getKey( k){
		var code = mapping[k];
	  return	keyClickStatus[code]
	}
	
	function setKey( e){
			var	selectKeyID = e.keyCode ? e.keyCode :e.which;
			keyClickStatus[selectKeyID]=!keyClickStatus[selectKeyID];
			
			 for(var i in keyClickStatus){
					 	  if(i==selectKeyID){
					 	 		continue;
					 	 	}
						 	if(selectKeyID>47&&selectKeyID<58){//数字按键
						 		if(i>47&&i<58){//其他数字按键清除
						 			 	keyClickStatus[i]=false;
						 		}
						 	}else{
						 		if(i<47||i>58){//字母命令只会有一个
						 			 	keyClickStatus[i]=false;
						 		}
						 		
						 	}
			 	}
				 
	}

    	
    function canvasMousedown(e) {
	    		onMouseDownX = e.clientX;
					onMouseDownY = e.clientY;
					selectCarrierChain = getPoint(e)
					if(selectCarrierChain){
							  selectCarrierChainBeginAnchor =selectCarrierChain.getAnchor();
								selectCarrierChain.doSelect();
								lastSelectCarrierChain = selectCarrierChain;
					}
		};
   function canvasMousemove(e) {
   	
		   	if(getKey("R")||getKey("L")){
		   		return;
		   	}
    	
	    	if(selectCarrierChain){
					var x=e.clientX;
					var y = e.clientY;
					var anchor =selectCarrierChain.getAnchor();
					selectCarrierChain.moveTo(x, y)
				}
    	
		};
		
   
    function canvasMouseup(e) {
	    	onMouseUpX = e.clientX;
				onMouseUpY = e.clientY;
				if(!selectCarrierChain){//select area
						for (var i=0;i<allPath.length;i++){
								var cc = allPath[i];
							  var point = 	cc.getAnchor();
								if(isIn(point.x,onMouseUpX,onMouseDownX)&&isIn(point.y,onMouseUpY,onMouseDownY) ){
									cc.clickPoint();
								}
					  }
					
				}else{
						if(getKey("R")){//角度
								myLoadData("sign/angle/"+selectCarrierChain.id,initStn)
						} else if(getKey("L")){//line
							  var upSelect = getPoint(e)
								myLoadData("sign/addLine/"+selectCarrierChain.id+"/"+upSelect.id,initStn);	
						} else	if(getKey("K")){//关键点
								myLoadData("sign/key/"+selectCarrierChain.id,initStn)
						}else{
							
							if(!selectCarrierChainBeginAnchor||!selectCarrierChainBeginAnchor.x||!selectCarrierChainBeginAnchor.y){
								return 
								}
								var moveX=onMouseUpX-selectCarrierChainBeginAnchor.x
								var moveY=onMouseUpY-selectCarrierChainBeginAnchor.y
								if(moveX!=0||moveY!=0){
									   
								   	var ids=[];
								    var xArr=[];
								    var yArr=[];
											for (var i=0;i<allPath.length;i++){
												var cc = allPath[i];
												if(cc.select&&cc.id!=selectCarrierChain.id){
													 cc.move(moveX,moveY);
													 ids.push(cc.id);
													 xArr.push(cc.x);
													 yArr.push(cc.y);
												}
								    }
								    ids.push(selectCarrierChain.id);
										xArr.push(selectCarrierChain.x);
										yArr.push(selectCarrierChain.y);
								
								    updateDBPos(ids,xArr,yArr);
									
								}
						} 
					   		
				}
				
    	selectCarrierChain=null;
    	selectCarrierChainBeginAnchor=null;
		};

  function canvasKeyup(){
  	   if(getKey("+")||getKey("-")){
  	   	 	var ids=[];
  	   	 	var lenArr=[];
  	   	 	var len=0;
		    	for (var i=0;i<allPath.length;i++){
								var cc = allPath[i];
								if(cc.select){
									if(len==0){
									   len =len==0? cc.length:len;
									   if(cc.rotateAngle==90||cc.rotateAngle==270){
									   	
									   				   len=toX(len);
									   }else{
									   				   len=toY(len);
									   		
									   }
						
									 }
							
									 lenArr.push(len);
									 ids.push(cc.id);
								}
					 }
					 if(ids.length==0){
					 	return;
					 	}
					 
					 	myLoadData("sign/update/"+ids+"/0/0/"+lenArr,initStn)
		    	
			 }
  	
  	
    }
  
   function canvasKeydown(e) {
		  setKey( e)
			selectCarrierChain=null;
			
			if( getKey("A")){
				var name=prompt("站台编号","编号");
				if(name){
						myLoadData("sign/addStn/"+name+"/"+toX(onMouseUpX)+"/"+toY(onMouseUpY),initStn)
				}
			}
			 if(getKey("+")){//水平对齐
		    		for (var i=0;i<allPath.length;i++){
								var cc = allPath[i];
								if(cc.select){
									cc.setLength(cc.length+1)
									 
								}
					  }
			 }
			 if(getKey("-")){//水平对齐
		    	for (var i=0;i<allPath.length;i++){
								var cc = allPath[i];
								if(cc.select){
									cc.setLength(cc.length-1)
									 
								}
					  }
			 }
				
				
		   if(getKey("X")){//水平对齐
		    		clickKeyWork_X()
			 }
				
			 if(getKey("Y")){
					clickKeyWork_Y();
			 }
			 if(getKey("E")){
					clickKeyWork_E();
			 }
			  if(getKey("C")){
					clickKeyWork_C();
			 }
			
			
	  }
  function clickKeyWork_X(){
 	         
		    	var ids=[];
		    	var x=0
					for (var i=0;i<allPath.length;i++){
								var cc = allPath[i];
								if(cc.select){
									 ids.push(cc.id);
									 x+=cc.getAnchor().x
								}
					}
					var len = ids.length;
					if(len==0){
						return;
					}
					x/=len;
				  x=toX(x)
					myLoadData("sign/updateX/"+parseInt(x)+"/"+ids,initStn)
 	
 	}
 		  function clickKeyWork_E(){
 	        var max_x=0;
 	        var max_y=0;
 	        var min_x=300000;
 	        var min_y=300000;
		    	var selectPath=[];
		    	var distance = 0;
		    	var x=0
		    	var last =0;
					for (var i=0;i<allPath.length;i++){
								var cc = allPath[i];
								if(cc.select){
									 selectPath.push(cc);
									 var anthor =cc.getAnchor()
									 max_x=anthor.x >max_x?anthor.x:max_x;
									 max_y=anthor.y >max_y?anthor.y:max_y;
									 min_x=anthor.x <min_x?anthor.x:min_x;
									 min_y=anthor.y <min_y?anthor.y:min_y;
									 last =cc.distance;
									 distance+=cc.distance;
								}
					}
					distance-=last;
					
					var len = selectPath.length;
					if(len<2){
						return;
					}
					var perDistanceX=(max_x-min_x)/distance;
					var perDistanceY=(max_y-min_y)/distance;
					
					for(var i=0;i<selectPath.length;i++){
						  var disI =selectPath[i].getDis();
							for(var j=i+1;j<selectPath.length;j++){
							    var disJ=selectPath[j].getDis();
							    if(disI>disJ){
							    	var tmp =selectPath[i];
							    	selectPath[i]=selectPath[j];
							    	selectPath[j]=tmp;
							    	disI=disJ;
							    }
							}
					}
					var x =min_x;
					var y =min_y;
					for(var i=0;i<selectPath.length;i++){
						 var path =selectPath[i];
						 path.setLength(path.distance*(perDistanceX>perDistanceY?perDistanceX:perDistanceY));
						 path.moveTo(x,y);
						 x+=perDistanceX*path.distance
						 y+=perDistanceY*path.distance
					}
					
					var lengthArr=[];
		      var ids=[];
			    var xArr=[];
			    var yArr=[];
				  for (var i=0;i<selectPath.length;i++){
						   var cc = selectPath[i];
							 ids.push(cc.id);
							 xArr.push(cc.x);
							 yArr.push(cc.y);
							 var dbLenth =cc.length*30000/(perDistanceX>perDistanceY?screenWidth:screenHeight)/2
							 lengthArr.push(parseInt(dbLenth));
						
			    }
			    updateDBPos(ids,xArr,yArr,lengthArr);
 	
 	}
 
 	  function clickKeyWork_C(){
  	     var len =  lastSelectCarrierChain.length ;
			   if(lastSelectCarrierChain.rotateAngle%180==90){
			   			len=toX(len)  ;
			   }else{
			   			len=toY(len);
			   }
 	       
 	       var ids=[];
  	   	 var lenArr=[];
  	   	
		    for (var i=0;i<allPath.length;i++){
								var cc = allPath[i];
								if(cc.select){
									 lenArr.push(len);
									 ids.push(cc.id);
								}
				}
				 if(ids.length==0){
				 	return;
				 }
				 
				 	myLoadData("sign/update/"+ids+"/0/0/"+lenArr,initStn)
 	}
 
 function clickKeyWork_Y(){
 	          var ids=[];
			    	var y=0
						for (var i=0;i<allPath.length;i++){
									var cc = allPath[i];
									if(cc.select){
										 ids.push(cc.id);
										 y+=cc.getAnchor().y
									}
						}
						var len = ids.length;
						if(len==0){
							return;
						}
						y/=len;
					  y=toY(y)
						myLoadData("sign/updateY/"+parseInt(y)+"/"+ids,initStn)
 	
 	}
 	
 	function isIn(x,min,max){
				var d =(x-min)*(x-max);
				return d<0
}

	function updateDBPos(ids,xArr,yArr,len){
						 for(var i=0;i<ids.length;i++){
							  xArr[i]=toX(xArr[i]) 
							  yArr[i]=toY(yArr[i])
							
							}
							var url ="sign/update/"+ids+"/"+xArr+"/"+yArr+"/"
							if(len){
								url+=len;
							}else{	
								url	+="0";
						  }
							
							
							myLoadData(url,initStn);
					
					}
				
function getPoint(e){
			   var x=e.clientX;
					var y = e.clientY;
					for(var i=0;i<allPath.length;i++){
						var cc = allPath[i];
						if(cc.IsPointInMatrix({x:x,y:y})){
							return cc;
							}
					}	
			}
			
			
			